package com.example.f_code;

import static android.content.ContentValues.TAG;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.f_code.utils.PrefsUtil;

public class WelcomeActivity extends AppCompatActivity {
    private boolean isFirst;
    private TextView tvCountdown;
    private CountDownTimer countDownTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        // 获取首次打开的值
        isFirst = PrefsUtil.isFirstLaunch(this);
        // 获取右上角文本
        tvCountdown = findViewById(R.id.tv_countdown);
        Log.d(TAG, "onCreate: 是否首次启动" + isFirst);
        startCountDown();
    }

    // 启动页
    private void startCountDown() {
        countDownTimer = new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvCountdown.setText((millisUntilFinished / 1000) + "s");
            }

            @Override
            public void onFinish() {
                checkLaunchStatus();
            }
        }.start();
    }

    private void checkLaunchStatus() {
        if (isFirst) {
            startActivity(new Intent(this, RegisterActivity.class));
            PrefsUtil.setLaunched(this);
        } else {
            startActivity(new Intent(this, LoginActivity.class));
        }
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }
}